package com.ruoyi.user.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.domain.bo.UserRelationBo;
import com.ruoyi.user.domain.vo.UserRelationVo;
import com.ruoyi.user.service.IUserRelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 人员社会关系
 *
 * @author strongme
 * @date 2023-01-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/user/relation")
public class UserRelationController extends BaseController {

    private final IUserRelationService iUserRelationService;

    /**
     * 查询人员社会关系列表
     */
    @GetMapping("/list")
    public TableDataInfo<UserRelationVo> list(UserRelationBo bo, PageQuery pageQuery) {
        return iUserRelationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出人员社会关系列表
     */
    @Log(title = "人员社会关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserRelationBo bo, HttpServletResponse response) {
        List<UserRelationVo> list = iUserRelationService.queryList(bo);
        ExcelUtil.exportExcel(list, "人员社会关系", UserRelationVo.class, response);
    }

    /**
     * 获取人员社会关系详细信息
     *
     * @param relationId 主键
     */
    @GetMapping("/{relationId}")
    public R<UserRelationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long relationId) {
        return R.ok(iUserRelationService.queryById(relationId));
    }

    /**
     * 根据人员ID获取社会关系列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/listByUserId/{userId}")
    public R<List<UserRelationVo>> getInfosByUserId(@NotNull(message = "主键不能为空")
                                                    @PathVariable Long userId) {
        return R.ok(iUserRelationService.queryByUserId(userId));
    }

    /**
     * 新增人员社会关系
     */
    @Log(title = "人员社会关系", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserRelationBo bo) {
        return toAjax(iUserRelationService.insertByBo(bo));
    }

    /**
     * 修改人员社会关系
     */
    @Log(title = "人员社会关系", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserRelationBo bo) {
        return toAjax(iUserRelationService.updateByBo(bo));
    }

    /**
     * 删除人员社会关系
     *
     * @param relationIds 主键串
     */
    @Log(title = "人员社会关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{relationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] relationIds) {
        return toAjax(iUserRelationService.deleteWithValidByIds(Arrays.asList(relationIds), true));
    }
}
